package com.cookor.poi.modules.cms.service;

import com.cookor.poi.common.utils.IdGen;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.cms.dao.CmsQuestionDao;
import com.cookor.poi.modules.cms.entity.CmsQuestion;
import com.cookor.poi.modules.cms.entity.CmsQuestionExample;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class QuestionService  {

    @Resource
    private CmsQuestionDao cmsQuestionDao;

    public CmsQuestion get(String id){
        return cmsQuestionDao.selectByPrimaryKey(id);
    }

    public PageInfo<CmsQuestion> findPage(CmsQuestion question, String pageNum, String pageSize) {
        // 更新过期的权重，间隔为“6”个小时
//        Date updateExpiredWeightDate =  (Date) CacheUtils.get("updateExpiredWeightDateByArticle");
//        if (updateExpiredWeightDate == null || (updateExpiredWeightDate != null
//                && updateExpiredWeightDate.getTime() < new Date().getTime())){
//            dao.updateExpiredWeight(article);
//            CacheUtils.put("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));
//        }
        CmsQuestionExample questionExample = new CmsQuestionExample();
        CmsQuestionExample.Criteria criteria = questionExample.createCriteria();
        if(StringUtils.isBlank(pageNum)){
            pageNum = "1";
        }
        if(StringUtils.isBlank(pageSize)){
            pageSize = "15";
        }
        if(question != null && StringUtils.isNotBlank(question.getTitle())){
            criteria.andTitleLike("%" + question.getTitle() + "%");
        }
        if(question != null && StringUtils.isNotBlank(question.getDelFlag())){
            criteria.andDelFlagEqualTo(question.getDelFlag());
        }
        criteria.andDelFlagEqualTo(question.getDelFlag() == null ? "0" : question.getDelFlag());
        String orderBy = "weight desc"; //（排序字段 空格 排序方式,排序字段 空格 排序方式）
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize), orderBy);
//        Page<CmsQuestion> page = new Page<CmsQuestion>();
//        page.setOrderBy(orderBy);
//        return super.findPage(page, question);
        List<CmsQuestion> cmsQuestions = cmsQuestionDao.selectByExampleWithBLOBs(questionExample);
        PageInfo<CmsQuestion> page = new PageInfo<CmsQuestion>(cmsQuestions);
//        page.setList(cmsQuestions);
        return page;
    }

    @Transactional(readOnly = false)
    public void save(CmsQuestion question) {

        if (question.getContent()!=null){
            question.setContent(StringEscapeUtils.unescapeHtml4(
                    question.getContent()));
        }
        // 如果没有审核权限，则将当前内容改为待审核状态
        if (!UserUtils.getSubject().isPermitted("cms:article:audit")){
            question.setDelFlag("2");
        }
        // 如果栏目不需要审核，则将该内容设为发布状态
//        if (article.getCategory()!=null&&StringUtils.isNotBlank(article.getCategory().getId())){
//            Category category = categoryDao.get(article.getCategory().getId());
//            if (!Global.YES.equals(category.getIsAudit())){
//                article.setDelFlag("0");
//            }
//        }
        if(StringUtils.isBlank(question.getId())){
            question.setId(IdGen.uuid());
            question.setCreator(UserUtils.getUser().getName());
            question.setCreateDate(new Date());
            question.setUpdateDate(question.getCreateDate());
            cmsQuestionDao.insertSelective(question);
        }else{
            question.setUpdater(UserUtils.getUser().getName());
            question.setUpdateDate(new Date());
            cmsQuestionDao.updateByPrimaryKeySelective(question);
        }

    }

    @Transactional(readOnly = false)
    public void delete(CmsQuestion question) {
        cmsQuestionDao.updateByPrimaryKeySelective(question);
    }

    @Transactional(readOnly = false)
    public void updateHitsAddOne(String id) {
        cmsQuestionDao.updateHitsAddOne(id);
    }
}
